package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.AreaParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TdSysAreaService;
import com.onluy.sysman.entity.sysmgr.TdSysArea;

@Validated
@RestController
@RequestMapping("/area")
@Api(value="area", description="/地市信息管理接口", position=1 )
public class TdSysAreaController extends AppWalkController {


	//调用服务
	@Autowired
	private TdSysAreaService tdSysAreaService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据areaCode获取信息")
	public RspInfo<TdSysArea> dotarget(@RequestBody ReqInfo<ReqHead, AreaParam> reqInfo   ) throws Exception {
		RspInfo<TdSysArea> jsonResult = new RspInfo<TdSysArea>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TdSysArea tdSysArea = new TdSysArea();

		if (null !=  reqInfo.getReqBody().getAreaCode()) {
			tdSysArea.setAreaCode(reqInfo.getReqBody().getAreaCode()).asCondition();
		}
		else{
			tdSysArea.setAreaCode("-1").asCondition();
		}
		tdSysArea = tdSysAreaService.model(tdSysArea);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tdSysArea);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增部门")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TdSysArea> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TdSysArea tdSysArea = reqInfo.getReqBody();
		num = tdSysAreaService.add(tdSysArea);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据areaCode更新")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TdSysArea> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;

		if( null == reqInfo.getReqBody().getAreaCode() )throw new Exception("缺少必要属性areaCode！");

		TdSysArea tdSysArea = reqInfo.getReqBody();
		tdSysArea.setAreaCode(  reqInfo.getReqBody().getAreaCode()  ).asCondition();
		num=tdSysAreaService.update( tdSysArea);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据areaCode删除")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,AreaParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TdSysArea tdSysArea = new TdSysArea();
		int num=1;
		try {
			if( null == reqInfo.getReqBody().getAreaCode() )throw new Exception("缺少必要属性areaCode！");
			tdSysArea.setAreaCode(  reqInfo.getReqBody().getAreaCode()  ).asCondition();

			num=tdSysAreaService.del( tdSysArea);
		} catch (Exception e) {
			jsonResult.setRspDesc("删除异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="部门列表明细")
	public RspInfo<PageData<TdSysArea>>  doList(@RequestBody    ReqInfo<ParamPagination, TdSysArea> reqInfo )throws Exception {
		RspInfo<PageData<TdSysArea>> jsonResult=new RspInfo<PageData<TdSysArea>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TdSysArea> list =tdSysAreaService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TdSysArea tdSysArea){

		if(tdSysArea == null )return;

		if(null!=tdSysArea.getAreaCode()){
			tdSysArea.setAreaCode(tdSysArea.getAreaCode()).asCondition();
		}
		if(null!=tdSysArea.getAreaName()){
			tdSysArea.setAreaName(null).getCondition().andLike( "%"+tdSysArea.getAreaName()+"%" );
		}

	}


}
